package com.itheima.easy.handler.aliyun;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.common.models.*;
import com.itheima.easy.config.alipay.AliPayConfig;
import com.itheima.easy.constant.SuperConstant;
import com.itheima.easy.constant.TradingConstant;
import com.itheima.easy.enums.TradingEnum;
import com.itheima.easy.exception.ProjectException;
import com.itheima.easy.handler.CommonPayHandler;
import com.itheima.easy.utils.BeanConv;
import com.itheima.easy.utils.EmptyUtil;
import com.itheima.easy.utils.ExceptionsUtil;
import com.itheima.easy.utils.SnowflakeIdWorker;
import com.itheima.easy.vo.RefundRecordVo;
import com.itheima.easy.vo.TradingVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName AliCommonPayHandler.java
 * @Description 阿里支付基础支付
 */
@Slf4j
@Service
public class AliCommonPayHandler implements CommonPayHandler {

    @Autowired
    private Config config;

    @Override
    public TradingVo queryTrading(TradingVo tradingVo) {
		return null;
    }

    @Override
    public TradingVo refundTrading(TradingVo tradingVo) {
        //1、生成退款请求编号
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(1,1);
        tradingVo.setOutRequestNo(String.valueOf(snowflakeIdWorker.nextId()));
        
        return null;
    }

    @Override
    public RefundRecordVo queryRefundTrading(RefundRecordVo refundRecordVo) {
        
        return null;
    }

    @Override
    public TradingVo closeTrading(TradingVo tradingVo) {
        return null;
    }

    @Override
    public TradingVo downLoadBill(TradingVo tradingVo) {
        //1、获得支付宝配置文件
        //2、配置如果为空，抛出异常
        if (EmptyUtil.isNullOrEmpty(config)){
            throw new ProjectException(TradingEnum.CONFIG_ERROR);
        }
        //3、使用配置
        Factory.setOptions(config);
        try {
            AlipayDataDataserviceBillDownloadurlQueryResponse billDownloadurlQueryResponse = Factory.Payment.Common()
                .downloadBill(tradingVo.getBillType(), DateUtil.formatDate(tradingVo.getBillDate()));
            //4、请求下载受理情况
            boolean success = ResponseChecker.success(billDownloadurlQueryResponse);
            if (success){
                String billDownloadUrl = billDownloadurlQueryResponse.getBillDownloadUrl();
                tradingVo.setBillDownloadUrl(billDownloadUrl);
            }else {
                log.warn("网关：支付宝下载订单类型：{},时间：{},结果：{}", tradingVo.getBillType(),tradingVo.getBillDate(),
                        JSONObject.toJSONString(billDownloadurlQueryResponse));
                throw new RuntimeException("网关：支付宝下载订单失败！");
            }
        } catch (Exception e) {
            log.warn("支付宝下载订单失败：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw  new RuntimeException("支付宝下载订单失败！");
        }
        return tradingVo;
    }

}
